﻿using System;
using System.Data;
using System.Data.SqlClient;

namespace UDS.Components
{
    /// <summary>
    /// 部门处理类
    /// </summary>
    public class Department
    {
        /// <summary>
        /// 添加部门
        /// </summary>
        /// <param name="ParentDepartmentID">本部门的父部门id</param>
        /// <param name="name">本部门的名称</param>
        /// <param name="description">本部门的描述</param>
        /// <returns>本部门分配到的id</returns>
        public static long Add(long ParentDepartmentID, string name, string description)
        {
            var db = new Database();
            var parameters = new SqlParameter[3];

            parameters[0] = new SqlParameter();
            parameters[1] = new SqlParameter();
            parameters[2] = new SqlParameter();


            parameters[0].SqlDbType = SqlDbType.Int;
            parameters[0].ParameterName = "@Dept_ParentID";
            parameters[0].Value = ParentDepartmentID;


            parameters[1].SqlDbType = SqlDbType.NVarChar;
            parameters[1].Size = 255;
            parameters[1].ParameterName = "@Dept_Name";
            parameters[1].Value = name;


            parameters[2].SqlDbType = SqlDbType.Text;
            parameters[2].ParameterName = "@Dept_Remark";
            parameters[2].Value = description;


            //parameters[3] = new SqlParameter();
            //parameters[3].SqlDbType = SqlDbType.Int;
            //parameters[3].ParameterName = "@ID";
            //parameters[3].Direction = ParameterDirection.ReturnValue;

            return (db.RunProc("dbo.sproc_AddDepartment", parameters));
        }

        /// <summary>
        /// 添加考勤时间
        /// </summary>
        /// <param name="DepartmentID">部门id</param>
        /// <param name="ondutytime">上班时间</param>
        /// <param name="offdutytime">下班时间</param>
        /// <returns></returns>
        public static int AddDutyTime(long DepartmentID, DateTime ondutytime, DateTime offdutytime)
        {
            var db = new Database();
            var parameters = new[]
            {
                Database.MakeInParam("@departmentid", SqlDbType.Int, 4, DepartmentID),
                Database.MakeInParam("@ondutytime", SqlDbType.DateTime, 8, ondutytime),
                Database.MakeInParam("@offdutytime", SqlDbType.DateTime, 8, offdutytime)
            };
            return (db.RunProc("dbo.sproc_WA_AddDepartmentDutyTime", parameters));
        }

        /// <summary>
        /// 更改部门考勤时间
        /// </summary>
        /// <param name="DepartmentID">部门id</param>
        /// <param name="ondutytime">上班时间</param>
        /// <param name="offdutytime">下班时间</param>
        /// <returns></returns>
        public static int UpdateDutyTime(long DepartmentID, DateTime ondutytime, DateTime offdutytime)
        {
            var db = new Database();
            var parameters = new[]
            {
                Database.MakeInParam("@departmentid", SqlDbType.Int, 4, DepartmentID),
                Database.MakeInParam("@ondutytime", SqlDbType.NVarChar, 20, ondutytime.ToShortTimeString()),
                Database.MakeInParam("@offdutytime", SqlDbType.NVarChar, 20, offdutytime.ToShortTimeString())
            };
            return (db.RunProc("dbo.sproc_WA_UpdateDepartmentDutyTime", parameters));
        }

        public static int DeleteDutyTime(long DepartmentID)
        {
            var db = new Database();
            var parameters = new[]
            {
                Database.MakeInParam("@departmentid", SqlDbType.Int, 4, DepartmentID)
            };
            return (db.RunProc("dbo.sproc_WA_DeleteDepartmentDutyTime", parameters));
        }


        public static int Delete(long DepartmentID)
        {
            var db = new Database();
            var parameters = new[]
            {
                Database.MakeInParam("@Dept_id", SqlDbType.Int, 4, DepartmentID)
            };
            return (db.RunProc("dbo.sproc_DeleteDepartment", parameters));
        }

        public static int Modify(long DepartmentID, string dep_name, string dep_description)
        {
            var db = new Database();
            var parameters = new[]
            {
                Database.MakeInParam("@dept_id", SqlDbType.Int, 4, DepartmentID),
                Database.MakeInParam("@dept_name", SqlDbType.NVarChar, 300, dep_name),
                Database.MakeInParam("@dept_description", SqlDbType.NVarChar, 300, dep_description)
            };
            return (db.RunProc("dbo.sproc_UpdateDepartmentInfo", parameters));
        }

        public SqlDataReader GetDepartmentInfo(long DepartmentID)
        {
            var db = new Database();
            var parameters = new[]
            {
                Database.MakeInParam("@dept_id", SqlDbType.Int, 4, DepartmentID)
            };

            return db.RunDataReader("dbo.sproc_GetDepartmentInfo", parameters);
        }

        public SqlDataReader GetDepartmentDutyTime(long DepartmentID)
        {
            var db = new Database();
            var parameters = new[]
            {
                Database.MakeInParam("@DeptID", SqlDbType.Int, 4, DepartmentID)
            };
            return db.RunDataReader("dbo.sproc_GetDepartmentDutyTime", parameters);
        }

        public int MoveDeparement(long FromDepartmentID, long ToDepartmentID)
        {
            var db = new Database();
            var parameters = new[]
            {
                Database.MakeInParam("@FromDepartmentID", SqlDbType.Int, 4, FromDepartmentID),
                Database.MakeInParam("@ToDepartmentID", SqlDbType.Int, 4, ToDepartmentID),
            };
            return (db.RunProc("dbo.sproc_MoveDepartment", parameters));
        }
    }
}